﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Loong.Model;

namespace QYWebSiteBN
{
    public partial class Form_ContentRule : Form
    {
        private int index=-1;

        public Form_ContentRule()
        {
            InitializeComponent();
        }

        public Form_ContentRule(int index):this()
        {
            this.index=index;
            set(index);
        }

        private void set(int id)
        {
             TranslateContentRuleInfo ruleinfo=  TranslateContentRule.Get(id);
             txtRuleName.Text=ruleinfo.Name;
             txtExpre.Text = ruleinfo.Expressions;
             txtInclude.Text = ruleinfo.Include;
             txtUnInclude.Text = ruleinfo.UnInclude;
 
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRuleName.Text))
            {
                MessageBox.Show("名称不可能为空!");
                txtRuleName.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtExpre.Text))
            {
                MessageBox.Show("规则不能为空!");
                txtRuleName.Focus();
                return;
            }

            for (int i = 0; i < TranslateContentRule.Count; i++)
            {
                if (txtRuleName.Text.ToLower() == TranslateContentRule.Get(i).Name.ToLower()&&index<0)
                {
                    MessageBox.Show("名称已经存在!");
                    txtRuleName.Focus();
                    return;
                }
            }

            TranslateContentRuleInfo info = new TranslateContentRuleInfo(txtRuleName.Text, txtExpre.Text, txtInclude.Text, txtUnInclude.Text);
            if (index <0)
            {
                TranslateContentRule.Add(info);
            }
            else
            {
                TranslateContentRule.Edit(index, info);
            }

            Form_ContentRuleList rulelist = (Form_ContentRuleList)this.Owner;
            rulelist.UpdateList();
            this.Close();

         
        }

        private void btnCancal_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
